<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\News;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class NewsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new News(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('url')->display(function ($value)  {
                return route('article',['url'=>$value]);
            })->copyable();
            $grid->column('cur_status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->disableFilterButton();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new News(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('url','链接')->required()->help('链接地址名')->rules(function (Form $form) {

                if ($form->isEditing()) {
                    return 'unique:news,url,' . $form->model()->id;
                }else{
                    return 'unique:news';
                }

            });
;
            $form->editor('content');
            $form->radio('cur_status')->options([0 => '隐藏', 1 => '显示'])->default(0);
            $form->display('created_at');
            $form->display('updated_at');
            // 去掉`重置`按钮
            $form->disableResetButton();
            // 去掉`查看`checkbox
            $form->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $form->disableEditingCheck();
            $form->disableDeleteButton();
            $form->disableViewButton();
            // 去掉`继续创建`checkbox
            $form->disableCreatingCheck();
        });
    }
}
